//
//  TagPickerViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 28..
//  Copyright 2011 Daum. All rights reserved.
//

#import "TagPickerViewController.h"


@implementation TagPickerViewController
@synthesize tagPickerDataSource = _tagPickerDataSource;
@synthesize tagPickerView = _tagPickerView;
@synthesize showLabel = _showLabel;
@synthesize applyBtn = _applyBtn;
@synthesize cancelBtn = _cancelBtn;

/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tagPickerDataSource = [[TagPickerDataSource alloc] init];
    }
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [_applyBtn release];
    [_cancelBtn release];
    [_showLabel release];
    [_tagPickerDataSource release];
    [_tagPickerView release];
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"태그 선택";
    self.tagPickerView.dataSource = _tagPickerDataSource;
//    [self.showLabel setText:_tagPickerDataSource.tagName];    
    if([_tagPickerDataSource.tagPickerArray count]>0){
        [self.showLabel setText:[[_tagPickerDataSource.tagPickerArray objectAtIndex:[_tagPickerView selectedRowInComponent:0]] objectForKey:@"title"]];
    }else{
        [self.showLabel setText:@""];
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    self.applyBtn=nil;
    self.cancelBtn=nil;
    self.showLabel=nil;
    self.tagPickerDataSource = nil;
    self.tagPickerView = nil;
    [super viewDidUnload];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_tagPickerDataSource reloadData];
    [_tagPickerView reloadAllComponents];
    if([_tagPickerDataSource.tagPickerArray count]<=[_tagPickerView selectedRowInComponent:0]){
        [_tagPickerView selectRow:0 inComponent:0 animated:YES];
        [_resultDelegate applyResultSet:self resultDic:nil];
        [self.showLabel setText:@""];
    }else{
        [self.showLabel setText:[[_tagPickerDataSource.tagPickerArray objectAtIndex:[_tagPickerView selectedRowInComponent:0]] objectForKey:@"title"]];    
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma mark UIPickerViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//	if (pickerView == _tagPickerView){
//        _tagPickerDataSource.tagName = [[_tagPickerDataSource.tagPickerArray objectAtIndex:row] objectForKey:@"title"];
//        _tagPickerDataSource.tagId = [[_tagPickerDataSource.tagPickerArray objectAtIndex:row] objectForKey:@"tag_id"];
//	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    //    NSLog(@"titleForRow");
	NSString *returnStr = @"";
    returnStr = [[_tagPickerDataSource.tagPickerArray objectAtIndex:row] objectForKey:@"title"];            
    return returnStr;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    //    NSLog(@"rowHeightForComponent");
	return 40.0;
}
#pragma mark UIAlertViewDelegate mothods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    
    if(buttonIndex==0){
        [self openTagManage:nil];
    }
}


#pragma mark -
#pragma mark UIButtonDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)applyAct:(id)sender{
    NSLog(@"applyAct");
    if([_tagPickerDataSource.tagPickerArray count]==0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"웁스" message:@"목록이 없습니다.\n목록을 추가 하시겠습니까?." delegate:self cancelButtonTitle:nil otherButtonTitles:@"확인", @"아니오", nil];
        [alert show];	
        [alert release];                
    }else{
        [[_tagPickerDataSource.tagPickerArray objectAtIndex:[_tagPickerView selectedRowInComponent:0]] objectForKey:@"title"];
        [_resultDelegate applyResultSet:self resultDic:[NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                        [[_tagPickerDataSource.tagPickerArray objectAtIndex:[_tagPickerView selectedRowInComponent:0]] objectForKey:@"tag_id"], @"tagId",
                                                        [[_tagPickerDataSource.tagPickerArray objectAtIndex:[_tagPickerView selectedRowInComponent:0]] objectForKey:@"title"], @"tagName",
                                                        nil]];
//        [_targetTextField setText:_tagPickerDataSource.tagName];
//        [_targetDic setObject:_tagPickerDataSource.tagId forKey:@"tagId"];
//        [_targetDic setObject:_tagPickerDataSource.tagName forKey:@"tagName"];
        [[self navigationController] popViewControllerAnimated:YES];        
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)cancelAct:(id)sender{
    NSLog(@"cancelAct");
    [[self navigationController] popViewControllerAnimated:YES];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)openTagManage:(id)sender{
    TagSettingViewController *viewController = [[[TagSettingViewController alloc] initWithNibName:@"TagSettingView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];                    
}
@end
